<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="项目" v-bind="validateInfos.projectId">
	          <j-dict-select-tag v-model:value="formData.projectId" dictCode="tm_project,project_name,id" placeholder="请选择项目" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="系统" v-bind="validateInfos.projectSystemId">
	          <j-dict-select-tag v-model:value="formData.projectSystemId" dictCode="tm_project_system,system_name,id" placeholder="请选择系统" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="框架/预研" v-bind="validateInfos.projectType">
	          <j-dict-select-tag v-model:value="formData.projectType" dictCode="project_type" placeholder="请选择框架/预研" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="批次" v-bind="validateInfos.batchId">
	          <j-dict-select-tag v-model:value="formData.batchId" dictCode="tm_project_batch,batch_name,id" placeholder="请选择批次" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="评审阶段" v-bind="validateInfos.reviewStage">
	          <j-dict-select-tag v-model:value="formData.reviewStage" dictCode="review_stage" placeholder="请选择评审阶段" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="评审物" v-bind="validateInfos.reviewContent">
	          <j-dict-select-tag v-model:value="formData.reviewContent" dictCode="review_content" placeholder="请选择评审物" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="发起人" v-bind="validateInfos.promoter">
			  <j-select-user v-model:value="formData.promoter" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="测试" v-bind="validateInfos.participator">
			  <j-select-user v-model:value="formData.participator" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="产品" v-bind="validateInfos.prodParticipator">
			  <j-select-user v-model:value="formData.prodParticipator" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开发" v-bind="validateInfos.devParticipator">
			  <j-select-user v-model:value="formData.devParticipator" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="问题数量" v-bind="validateInfos.numberIssues">
            <a-input v-model:value="formData.numberIssues" placeholder="请输入问题数量" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开始时间" v-bind="validateInfos.startTime">
		        <a-date-picker placeholder="请选择开始时间"  v-model:value="formData.startTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="结束时间" v-bind="validateInfos.finishTime">
		        <a-date-picker placeholder="请选择结束时间"  v-model:value="formData.finishTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="评审结果" v-bind="validateInfos.meetingResult">
	          <j-dict-select-tag v-model:value="formData.meetingResult" dictCode="review_result" placeholder="请选择评审结果" :disabled="disabled"/>
          </a-form-item>
        </a-col>
<!--        <a-col :span="12">-->
<!--        </a-col>-->
        <a-col :span="12">
          <a-form-item label="会议纪要" v-bind="validateInfos.meetingContent" style="margin-left: -140px;width: 1070px">
	          <j-editor v-model:value="formData.meetingContent" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../ProjectMeeting.api';
  import { Form } from 'ant-design-vue';

  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    projectId: '',
    projectSystemId: '',
    projectType: '',
    batchId: '',
    reviewStage: '',
    reviewContent: '',
    promoter: '',
    participator: '',
    prodParticipator: '',
    devParticipator: '',
    numberIssues: '',
    startTime: '',
    finishTime: '',
    meetingResult: '',
    meetingContent: '',
    meetintFile: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    projectId: [{ required: true, message: '请输入项目!'},],
    projectSystemId: [{ required: true, message: '请输入系统!'},],
    projectType: [{ required: true, message: '请输入框架/预研!'},],
    batchId: [{ required: true, message: '请输入批次!'},],
    reviewStage: [{ required: true, message: '请输入评审阶段!'},],
    reviewContent: [{ required: true, message: '请输入评审物!'},],
    promoter: [{ required: true, message: '请输入发起人!'},],
    participator: [{ required: true, message: '请输入测试!'},],
    numberIssues: [{ required: true, message: '请输入问题数量!'},],
    startTime: [{ required: true, message: '请输入开始时间!'},],
    finishTime: [{ required: true, message: '请输入结束时间!'},],
    meetingResult: [{ required: true, message: '请输入评审结果!'},],
    meetingContent: [{ required: true, message: '请输入会议纪要!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });


  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 850px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
